/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package spud;

import java.io.File;
import java.io.FileFilter;
import java.io.FileNotFoundException;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import spud.util.StringUtils;
import spud.util.SymbolExchangeUtils;
import spud.util.SymbolExchangeUtils.SymbolExchange;

/**
 *
 * @author bsmith
 */
public class FileHandler {

    private File directory = null;
    /**
     * All keys - cba, CBA, cba.ax, CBA.AX; Maps to file for it
     */
    private Map<String, File> filesMap = new HashMap<String, File>();
    /**
     * Just symbols - cba, bhp, ...; Maps to file for it
     */
    private Map<String, File> symbolsMap = new HashMap<String, File>();
    SymbolExchangeUtils symbolExchangeUtils = new SymbolExchangeUtils();

    public FileHandler(String dataDir) throws FileNotFoundException {
        if (!StringUtils.isValued(dataDir)) {
            throw new IllegalArgumentException("dataDir argument null or empty: '" + dataDir + "'");
        }
        directory = new File(dataDir);
        if (!directory.isDirectory()) {
            throw new FileNotFoundException("dataDir doesn't exist:" + dataDir);
        }
        buildFilesMap();
    }

    private void buildFilesMap() {
        File[] files = directory.listFiles(new FileHandlerFilter());
        List filesList = Arrays.asList(files);
        Iterator<File> filesIterator = filesList.iterator();

        for (; filesIterator.hasNext();) {
            File f = filesIterator.next();
            // format of files is CBA.AX.csv, CBA.csv, cba.ax.csv, cba.csv - if no exchange then default to one based on Locale
            SymbolExchange symbolExchange = symbolExchangeUtils.getSymbolExchange(f.getName());
            addToFilesMap(symbolExchange.getSymbol(), symbolExchange.getExchange(), f);
        }

    }

    private void addToFilesMap(String symbol, String exchange, File file) {
        // Want keys 'cba', 'CBA', 'cba.ax', 'CBA.AX'
        addMapEntry(symbol.toLowerCase() + "." + exchange.toLowerCase(), file);
        addMapEntry(symbol.toUpperCase() + "." + exchange.toUpperCase(), file);
        addMapEntry(symbol.toLowerCase(), file);
        addMapEntry(symbol.toUpperCase(), file);
        symbolsMap.put(symbol.toLowerCase(), file);
    }

    private void addMapEntry(String key, File file) {
        System.out.println("-> addMapEntry(key:" + key + ", File:" + file.getName());
        filesMap.put(key, file);
    }

    public Set<String> getFilesSymbols() {
        // Return list of symbols we have files for
        return symbolsMap.keySet();
    }

    /**
     *
     * @param symbol - may include the exchange as in CBA.AX or just CBA (infer
     * exchange based on Locality information). Case is irrelevant.
     * @return file that is named <symbol>[.exchange].csv.
     */
    File getFile(String key) {
        return filesMap.get(key);
    }

    private class FileHandlerFilter implements FileFilter {

        @Override
        public boolean accept(File file) {
            return (file.getName().endsWith(".csv") && file.length() > 0);
        }
    }
}
